package cn.iocoder.yudao.framework.common.genCode.common;

import cn.hutool.core.swing.clipboard.ClipboardUtil;

import java.util.Arrays;
import java.util.List;

public class GenBuildCode {

    public static void main(String[] args) {
        String code = getCode();
        System.out.println(code);
        ClipboardUtil.setStr(code);
    }

    public static String getCodeExample() {
        return "";
    }

    public static String getCode() {
        StringBuilder code = new StringBuilder();
        String codeExample = getCodeExample();
        String[] split = codeExample.split("\n");
        List<String> list = Arrays.asList(split);

        for (String s : list) {
            s = s.replace("\"", "\\\"");
            s = s.replace("#[[", "");
            s = s.replace("]]#", "");

            // globalBinding
            s = globalBinding(s);

            // localBinding
            s = localBinging(s);

            // localBinding.sceneEnum
            s = localBinding_sceneEnum(s);

            // table
            s = table(s);

            //primaryColumn
            s = primaryColumn(s);

            // column
            s = column(s);

            // javaField
            s = s.replace("$JavaField", "\" + StrUtil.upperFirst(column.getJavaField()) + \"");
            s = s.replace("${JavaField}", "\" + StrUtil.upperFirst(column.getJavaField()) + \"");
            // index.vue
            s = s.replace("$AttrName", "\" + StrUtil.upperFirst(column.getJavaField()) + \"");
            s = s.replace("${AttrName}", "\" + StrUtil.upperFirst(column.getJavaField()) + \"");

            // js
            s = s.replace("${baseURL}", "\"+\"/" + "\" + table.getModuleName() + \"" + "/" + "\" + localBinding.getSimpleClassName_strikeCase() + \"");


            if (s.equals("\n") || s.equals("")) {
                code.append("code.bl();");
            } else {
                code.append(String.format("code.apAndBl(\"%s\");", s));
            }
            code.append("\n");
        }
        return code.toString();
    }

    private static String globalBinding(String s) {
        s = s.replace("${basePackage}", "\" + globalBinding.getBasePackage() + \"");
        s = s.replace("${baseFrameworkPackage}", "\"+globalBinding.getBaseFrameworkPackage()+\"");
        s = s.replace("${CommonResultClassName}", "\" + globalBinding.getCommonResultClassName() + \"");
        s = s.replace("${PageResultClassName}", "\" + globalBinding.getPageResultClassName() + \"");
        s = s.replace("${PageParamClassName}", "\" + globalBinding.getPageParamClassName() + \"");
        s = s.replace("${DictFormatClassName}", "\" + globalBinding.getDictFormatClassName() + \"");
        s = s.replace("${BaseDOClassName}", "\" + globalBinding.getBaseDOClassName() + \"");
        s = s.replace("${QueryWrapperClassName}", "\" + globalBinding.getQueryWrapperClassName() + \"");
        s = s.replace("${BaseMapperClassName}", "\" + globalBinding.getBaseMapperClassName() + \"");
        s = s.replace("${ServiceExceptionUtilClassName}", "\" + globalBinding.getServiceExceptionUtilClassName() + \"");
        s = s.replace("${DateUtilsClassName}", "\" + globalBinding.getDateUtilsClassName() + \"");
        s = s.replace("${ExcelUtilsClassName}", "\" + globalBinding.getExcelUtilsClassName() + \"");
        s = s.replace("${ObjectUtilsClassName}", "\" + globalBinding.getObjectUtilsClassName() + \"");
        s = s.replace("${DictConvertClassName}", "\" + globalBinding.getDictConvertClassName() + \"");
        s = s.replace("${OperateLogClassName}", "\" + globalBinding.getOperateLogClassName() + \"");
        s = s.replace("${OperateTypeEnumClassName}", "\" + globalBinding.getOperateTypeEnumClassName() + \"");
        s = s.replace("${baseDOFields}", "\" + globalBinding.getBaseDOFields() + \"");
        return s;
    }

    private static String localBinding_sceneEnum(String s) {
        s = s.replace("${sceneEnum.basePackage}", "\" + localBinding.getSceneEnum().getBasePackage() + \"");
        s = s.replace("${sceneEnum.prefixClass}", "\" + localBinding.getSceneEnum().getPrefixClass() + \"");
        s = s.replace("${sceneEnum.name}", "\" + localBinding.getSceneEnum().getName() + \"");
        s = s.replace("${sceneEnum.scene}", "\" + localBinding.getSceneEnum().getScene() + \"");
        return s;
    }

    private static String localBinging(String s) {
        s = s.replace("${simpleClassName}", "\" + localBinding.getSimpleClassName() + \"");
        if (s.contains("${simpleClassName_underlineCase.toUpperCase()}")) {
            s = s.replace("${simpleClassName_underlineCase.toUpperCase()}", "\"+ localBinding.getSimpleClassName_underlineCase().toUpperCase() +\"");
        } else {
            s = s.replace("${simpleClassName_underlineCase}", "\"+ localBinding.getSimpleClassName_underlineCase() +\"");
        }
        s = s.replace("${classNameVar}", "\" + localBinding.getClassNameVar() + \"");
        s = s.replace("${simpleClassName_strikeCase}", "\" + localBinding.getSimpleClassName_strikeCase() + \"");
        s = s.replace("${permissionPrefix}", "\" +localBinding.getPermissionPrefix() + \"");
        return s;
    }

    private static String table(String s) {
        s = s.replace("${table.id}", "\" + table.getId() + \"");
        s = s.replace("${table.importType}", "\" + table.getImportType() + \"");
        s = s.replace("${table.scene}", "\" + table.getScene() + \"");
        s = s.replace("${table.tableName}", "\" + table.getTableName() + \"");
        s = s.replace("${table.tableComment}", "\" + table.getTableComment() + \"");
        s = s.replace("${table.remark}", "\" + table.getRemark() + \"");
        s = s.replace("${table.moduleName}", "\" + table.getModuleName() + \"");
        s = s.replace("${table.businessName}", "\" + table.getBusinessName() + \"");
        s = s.replace("${table.className}", "\" + table.getClassName() + \"");
        s = s.replace("${table.classComment}", "\" + table.getClassComment() + \"");
        s = s.replace("${table.author}", "\" + table.getAuthor() + \"");
        s = s.replace("${table.templateType}", "\" + table.getTemplateType() + \"");
        s = s.replace("${table.parentMenuId}", "\" + table.getParentMenuId() + \"");
        return s;
    }

    private static String primaryColumn(String s) {
        s = s.replace("${primaryColumn.id}", "\" + primaryColumn.getId() + \"");
        s = s.replace("${primaryColumn.tableId}", "\" + primaryColumn.getTableId() + \"");
        s = s.replace("${primaryColumn.columnName}", "\" + primaryColumn.getColumnName() + \"");
        s = s.replace("${primaryColumn.columnType}", "\" + primaryColumn.getColumnType() + \"");
        s = s.replace("${primaryColumn.columnComment}", "\" + primaryColumn.getColumnComment() + \"");
        s = s.replace("${primaryColumn.nullable}", "\" + primaryColumn.getNullable() + \"");
        s = s.replace("${primaryColumn.primaryKey}", "\" + primaryColumn.getPrimaryKey() + \"");
        s = s.replace("${primaryColumn.autoIncrement}", "\" + primaryColumn.getAutoIncrement() + \"");
        s = s.replace("${primaryColumn.ordinalPosition}", "\" + primaryColumn.getOrdinalPosition() + \"");
        s = s.replace("${primaryColumn.javaType}", "\" + primaryColumn.getJavaType() + \"");
        s = s.replace("${primaryColumn.javaField}", "\" + primaryColumn.getJavaField() + \"");
        s = s.replace("${primaryColumn.dictType}", "\" + primaryColumn.getDictType() + \"");
        s = s.replace("${primaryColumn.example}", "\" + primaryColumn.getExample() + \"");
        s = s.replace("${primaryColumn.createOperation}", "\" + primaryColumn.getCreateOperation() + \"");
        s = s.replace("${primaryColumn.updateOperation}", "\" + primaryColumn.getUpdateOperation() + \"");
        s = s.replace("${primaryColumn.listOperation}", "\" + primaryColumn.getListOperation() + \"");
        s = s.replace("${primaryColumn.listOperationCondition}", "\" + primaryColumn.getListOperationCondition() + \"");
        s = s.replace("${primaryColumn.listOperationResult}", "\" + primaryColumn.getListOperationResult() + \"");
        s = s.replace("${primaryColumn.htmlType}", "\" + primaryColumn.getHtmlType() + \"");
        return s;
    }

    private static String column(String s) {
        s = s.replace("${column.id}", "\" + column.getId() + \"");
        s = s.replace("${column.tableId}", "\" + column.getTableId() + \"");
        s = s.replace("${column.columnName}", "\" + column.getColumnName() + \"");
        s = s.replace("${column.columnType}", "\" + column.getColumnType() + \"");
        s = s.replace("${column.columnComment}", "\" + column.getColumnComment() + \"");
        s = s.replace("${column.nullable}", "\" + column.getNullable() + \"");
        s = s.replace("${column.primaryKey}", "\" + column.getPrimaryKey() + \"");
        s = s.replace("${column.autoIncrement}", "\" + column.getAutoIncrement() + \"");
        s = s.replace("${column.ordinalPosition}", "\" + column.getOrdinalPosition() + \"");
        s = s.replace("${column.javaType}", "\" + column.getJavaType() + \"");
        s = s.replace("${column.javaField}", "\" + column.getJavaField() + \"");
        s = s.replace("${column.dictType}", "\" + column.getDictType() + \"");
        s = s.replace("${column.example}", "\" + column.getExample() + \"");
        s = s.replace("${column.createOperation}", "\" + column.getCreateOperation() + \"");
        s = s.replace("${column.updateOperation}", "\" + column.getUpdateOperation() + \"");
        s = s.replace("${column.listOperation}", "\" + column.getListOperation() + \"");
        s = s.replace("${column.listOperationCondition}", "\" + column.getListOperationCondition() + \"");
        s = s.replace("${column.listOperationResult}", "\" + column.getListOperationResult() + \"");
        s = s.replace("${column.htmlType}", "\" + column.getHtmlType() + \"");

        // index.vue
        if (s.contains("$dictType.toUpperCase()")) {
            s = s.replace("$dictType.toUpperCase()", "\" + column.getDictType().toUpperCase() + \"");
        } else {
            s = s.replace("$dictType", "\" + column.getDictType() + \"");
        }
        s = s.replace("${dictType}", "\" + column.getDictType() + \"");
        s = s.replace("$javaField", "\" + column.getJavaField() + \"");
        s = s.replace("${javaField}", "\" + column.getJavaField() + \"");
        s = s.replace("$comment", "\" + column.getColumnComment() + \"");
        s = s.replace("${comment}", "\" + column.getColumnComment() + \"");
        return s;
    }

}
